//
//  YLCertifiedPaymentViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/2.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLCertifiedPaymentViewModel.h"
#import "YLGlobeHeader.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import "NSString+Regex.h"
#import "YLAccountTool.h"

@interface YLCertifiedPaymentViewModel ()
@property (nonatomic, strong) dispatch_source_t disTimer;
@end

@implementation YLCertifiedPaymentViewModel

//- (NSAttributedString *)agreementAttributedString
//{
//    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"确认同意 " attributes:@{NSForegroundColorAttributeName : YLLineColor}];
//    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"支付服务协议" attributes:@{NSForegroundColorAttributeName : YLLOrgColor}]];
//    return attStr;
//}

- (instancetype)init
{
    if (self = [super init]) {
        [self setUp];
    }
    return self;
}

//- (BOOL)canSendVerificationCode
//{
//    BOOL send = NO;
//    if (self.iDCardText.length == 0) {
//        [WHProgressHUD showError:@"请输入身份证号"];
//    } else if (self.nameText.length == 0) {
//        [WHProgressHUD showError:@"请输入银行卡号"];
//    } else if (self.bankCardText.length == 0) {
//        [WHProgressHUD showError:@"请输入银行卡号"];
//    } else if (![self.phoneNumberText isValidPhoneNumber]) {
//        [WHProgressHUD showError:@"请输入正确的手机号"];
//    } else {
//        send = YES;
//    }
//
//    return send;
//}
- (void)setUp
{
    //[self sendVerificationCode];
    
    [self pay];
    
    _sureEnableSiganl = [RACSignal combineLatest:@[RACObserve(self, nameText), RACObserve(self, phoneNumberText), RACObserve(self, bankCardText), RACObserve(self, iDCardText) ,] reduce:^id _Nullable(NSString *nameText, NSString *phoneNumberText, NSString *bankCardText, NSString *iDCardText){
        
        return @(nameText.length && phoneNumberText.length && bankCardText.length && iDCardText.length);
    }];
}
//
//- (void)sendVerificationCode
//{
//    __weak typeof(self) weakSelf = self;
//    // 发送验证码
//    _verificationCodeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
//        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
//
//            if (![weakSelf canSendVerificationCode]) {
//                [subscriber sendCompleted];
//                return nil;
//            }
//
//            if (weakSelf.disTimer) {
//                dispatch_source_cancel(weakSelf.disTimer);
//            }
//
//            NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:4];
//            params[@"BankCardCode"] = weakSelf.bankCardText;
//            params[@"Name"] = weakSelf.nameText;
//            params[@"IdentityId"] = weakSelf.idCardText;
//            params[@"BankTel"] = weakSelf.phoneNumberText;
//
//            NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetCusotmerPrivilege", YLURL];
//
//            [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
//
//                if ([responseObject[@"httpCode"] integerValue] == 200) {
//
//                } else {
//
//                    if (weakSelf.disTimer) {
//                        dispatch_source_cancel(weakSelf.disTimer);
//                    }
//
//                    [WHProgressHUD showError:responseObject[@"message"]];
//                    [subscriber sendNext:@"发验证码"];
//                }
//
//                [subscriber sendCompleted];
//            } fail:^(NSError *error) {
//
//                if (weakSelf.disTimer) {
//                    dispatch_source_cancel(weakSelf.disTimer);
//                }
//
//                [subscriber sendNext:@"发验证码"];
//                [subscriber sendCompleted];
//            }];
//
//
//            // 倒计时
//            {
//                __block int timeout = 59;
//                dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//                dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
//                weakSelf.disTimer = _timer;
//                dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
//                dispatch_source_set_event_handler(_timer, ^{
//                    if(timeout<=0){ //倒计时结束，关闭
//                        dispatch_source_cancel(_timer);
//                        dispatch_async(dispatch_get_main_queue(), ^{
//                            [subscriber sendNext:@"发验证码"];
//                            [subscriber sendCompleted];
//                        });
//                    }else{
//                        int seconds = timeout % 60;
//                        NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
//                        dispatch_async(dispatch_get_main_queue(), ^{
//                            [subscriber sendNext:[NSString stringWithFormat:@"%@秒后重新发送",strTime]];
//                        });
//                        timeout--;
//                    }
//                });
//
//                dispatch_resume(_timer);
//            }
//
//                return nil;
//        }];
//    }];
//
//}

- (void)pay
{

    __weak typeof(self) weakSelf = self;
    _payCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            [WHProgressHUD showStatus:@"正在认证中..."];
            NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:5];
            params[@"BankCardCode"] = weakSelf.bankCardText;
            params[@"Name"] = weakSelf.nameText;
            params[@"IdentityId"] = weakSelf.iDCardText;
            params[@"BankTel"] = weakSelf.phoneNumberText;
//            params[@"VerifyCode"] = weakSelf.codeText;
            
            NSString *url = [NSString stringWithFormat:@"%@AppCommon/CustomerAuth", YLURL];
            
            [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
                [WHProgressHUD dismiss];
                if ([responseObject[@"httpCode"] integerValue] == 200) {
                    [subscriber sendNext:nil];
                } else {
                    [WHProgressHUD showError:responseObject[@"message"]];
                }
                
                [subscriber sendCompleted];
            } fail:^(NSError *error) {
        
                [subscriber sendCompleted];
            }];
                
            return nil;
        }];
    }];
    
    
}


- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
    if (_disTimer) {
        dispatch_source_cancel(_disTimer);
    }
}

@end
